package com.BlackMesa.models;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


import com.BlackMesa.database.DatabaseAdapter;


import com.google.android.maps.GeoPoint;


import android.location.Location;
import android.util.Log;

/**
 * Holds the description, location, due date, and category for the task.
 * @author Tyler Bishop, Nick Incardone
 * @version 1.1
 */
public class Task implements Serializable{

	private OurLocation location;
	private String name;
	private String detailedDescription;
	private Category category;
	private Date dueDate, dueTime;
	private boolean isComplete;
	private int id;
	private int userid;
	
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * Sets up a new task with the given parameters.
	 * @param location
	 * 		location of tasks completion
	 * @param name
	 * 		a description of the task
	 * @param category
	 * 		the category the task belongs to; null if no category
	 * @param dueDate
	 * 		the date for the task to be complete
	 */
	public Task(String name,OurLocation location, Category category,
			Date dueDate, Date dueTime, String detailedDescription) {
		this.location = location;
		this.name = name;
		this.category = category;
		this.dueDate = dueDate;
		this.dueTime = dueTime;
		this.detailedDescription = detailedDescription;
		isComplete = false;
	}
	
	public Task(int id, String name, String dueDate, String detailedDescription, String isComplete, int location_id, int category_id, int user_id, String dueTime, DatabaseAdapter adapter) {
		this.id = id;
		this.name = name;
		this.dueDate = stringToDate(dueDate);
		this.detailedDescription = detailedDescription;
		this.dueTime = stringToTime(dueTime);
		Log.d("catid", Integer.toString(category_id));
		this.category = adapter.getCategory(category_id);
		this.userid = user_id;
		this.location = adapter.getLocation(location_id);
		if (isComplete.charAt(0) == 't') {
			this.isComplete = true;
		} else {
			this.isComplete = false;
		}
		
	}
	
	private Date stringToDate(String date) {
		//TODO
		return null;
	}
	
	private Date stringToTime(String time) {
		//TODO
		return null;
	}
	
	public Task(String name) {
		this.name = name;
		location = null;
		category = null;
		dueDate = null;
		dueTime = null;
		detailedDescription = null;
	}
	public Task() {
		name = "Name";
		try {
			dueDate = (new SimpleDateFormat("MM/dd/yyyy")).parse("04/12/2012");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public Date getDueDate() {
		return dueDate;
	}

	public void setDueDate(Date dueDate) {
		this.dueDate = dueDate;
	}

	public boolean isComplete() {
		return isComplete;
	}

	public void setComplete(boolean isComplete) {
		this.isComplete = isComplete;
	}
	
	public void switchComplete() {
		isComplete = !isComplete;
		
	}

	public String getDetailedDescription() {
		return detailedDescription;
	}

	public void setDetailedDescription(String detailedDescription) {
		this.detailedDescription = detailedDescription;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public boolean equals(Object that) {
		return this.name.equals(((Task)that).name);
	}

	public Date getDueTime() {
		return dueTime;
	}

	public void setDueTime(Date dueTime) {
		this.dueTime = dueTime;
	}

	public OurLocation getLocation() {
		if(location == null) {
			return new OurLocation("",new GeoPoint(0,0));
		}
		return location;
	}

	public void setLocation(OurLocation location) {
		this.location = location;
	}

	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}
	
}
